package com.example.sls.dao.user;

import com.example.sls.pojo.user.Notifications;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 消息通知表(Notifications)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:13
 */
public interface NotificationsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from notifications where id=#{id}")
  Notifications queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param notifications 查询条件
   * @param pageSize      分页对象
   * @param offset        偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from notifications where 1=1" +
          " and (id=#{notifications.id} or (isnull(#{notifications.id}) or #{notifications.id}=0))" +
          " and (user_id=#{notifications.userId} or (isnull(#{notifications.userId}) or #{notifications.userId}=0))" +
          " and (type=#{notifications.type} or (isnull(#{notifications.type}) or #{notifications.type}=''))" +
          " and (content=#{notifications.content} or (isnull(#{notifications.content}) or #{notifications.content}=''))" +
          " and (is_read=#{notifications.isRead} or (isnull(#{notifications.isRead}) or #{notifications.isRead}=0))" +
          " and (created_at=#{notifications.createdAt} or (isnull(#{notifications.createdAt}) or #{notifications.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Notifications> queryAllByLimit(Notifications notifications, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from notifications")
  long count();

  /**
   * 新增数据
   *
   * @param notifications 实例对象
   * @return 影响行数
   */
  @Insert("insert into notifications (id,user_id,type,content,is_read,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{type}!='' and !isnull(#{type}),#{type},null)," +
          "if(#{content}!='' and !isnull(#{content}),#{content},null)," +
          "if(#{isRead}!=0 and !isnull(#{isRead}),#{isRead},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Notifications notifications);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param notifications 实例对象
   * @return 影响行数
   */
  @Update("update notifications set " +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "type=if(isnull(#{type}) or #{type}='',type,#{type})," +
          "content=if(isnull(#{content}) or #{content}='',content,#{content})," +
          "is_read=if(isnull(#{isRead}) or #{isRead}=0,is_read,#{isRead})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Notifications notifications);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from notifications where id=#{id}")
  int deleteById(Integer id);
}

